123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #include "main.h"
- #include "usart.h"
- #include "gpio.h"
- #define ApplicationAddress 0x8010000
- #define VECT_TAB_OFFSET 0x10000
- int i = 0;
- int rf = 0;
- int row = 0;
- int data_count = 0;
- uint8_t msg[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x0D};
- uint8_t NACK[1]={0x79};
- uint32_t temp[1000];
- uint32_t FlashData1[1000];
- uint8_t FlashData[16];
- uint8_t com[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x0D};
- uint8_t com1[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x02, 0x0D};
- uint8_t start[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x03, 0x0D};
- void SystemClock_Config(void);
- void UserAppStart(void);
- void writeFlashTest(void);
- void printFlashTest(void);
- int main(void) {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_USART2_UART_Init;
- MX_UART4_Init;
- MX_USART1_UART_Init;
- HAL_UART_Transmit(&huart4,(uint8_t*)start,sizeof(start),10);
- HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_SET);
- SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
- while (1) {
- if (rf == 0) {
- HAL_UART_Receive_IT(&huart4, (uint8_t *)msg,sizeof(msg));
- } else {
- HAL_UART_Receive_IT(&huart4, (uint8_t *)FlashData,sizeof(FlashData));
- }
- }
- return 0;
- }
- void SystemClock_Config(void) {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 72;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 3;
- if ((HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
- Error_Handler();
- }
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
- Error_Handler();
- }
- }
- void UserAppStart(void) {
- typedef void (*pFunction)(void);
- pFunction Jump_To_Application;
- uint32_t JumpAddress=0;
- if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000) {
- JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
- Jump_To_Application = (pFunction) JumpAddress;
- __set_MSP(*(__IO uint32_t*) ApplicationAddress);
- Jump_To_Application();
- }
- }
- void writeFlashTest(void) {
- FLASH_EraseInitTypeDef pEraseInit;
- HAL_FLASH_Unlock();
- pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
- pEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
- pEraseInit.Sector = FLASH_SECTOR_4;
- pEraseInit.NbSectors = 1;
- uint32_t PageError=0;
- HAL_FLASHEx_Erase(&pEraseInit,&PageError);
- for (i=1; i<(data_count * 4); i++) {
- HAL_FLASH_Program(TYPEPROGRAM_WORD,ApplicationAddress+(i*4),FlashData1[i]);
- }
- HAL_FLASH_Lock();
- }
- void printFlashTest(void) {
- for (i=1; i<(data_count * 4); i++) {
- temp[i] = *(__IO uint32_t*)(ApplicationAddress+(i*4));
- }
- }
|